home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / checkIP.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  2.5 KB  |  80 lines

  1. #
  2. #  checkIP.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 2/1/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. #!/usr/bin/env python
  24.  
  25. import socket, threading
  26.  
  27.  
  28. class GetHostByNameThread (threading.Thread):
  29.     def __init__ (self, ipr):        
  30.         threading.Thread.__init__(self,target = self._myRun)
  31.         self.result = None
  32.         self.ipr = ipr
  33.     def _myRun (self):
  34.         try: self.result = socket.gethostbyname(self.ipr)
  35.         except: pass
  36.  
  37.  
  38. def checkIP (ip, host, timeout):
  39.     """Queries a single blacklist host about a single IP, using the specified
  40.     timeout value; returns True iff the IP is an open relay."""
  41.     ipl = ip.split('.')
  42.     ipl.reverse()
  43.     ipr = "%s.%s" % ('.'.join(ipl), host)
  44.  
  45.     # start a thread for gethostbyname(), and quits waiting for it if time is up
  46.     getHostByNameThread = GetHostByNameThread(ipr)
  47.     getHostByNameThread.start()
  48.     if timeout: getHostByNameThread.join(timeout)
  49.     else: getHostByNameThread.join()
  50.  
  51.     if getHostByNameThread.result and getHostByNameThread.result.startswith('127.'):
  52.         return True
  53.     
  54.     return False
  55.  
  56. def checkIPList (ipList, timeout, hostList):
  57.     """Checks a list IPs using the blacklist 'hostList'; returns None if no open relay
  58.     is found; otherwise returns a tuple (ip, host)."""
  59.     for ip in ipList:
  60.         for host in hostList:
  61.             if checkIP(ip, host, timeout):
  62.                 return (ip, host)
  63.             
  64.     return None
  65.  
  66.  
  67. if __name__ == "__main__":
  68.     import time
  69.  
  70.     HOST = 'bl.spamcop.net'
  71.     
  72.     startTime = time.time()
  73.     print "66.103.45.5:",checkIP("66.103.45.5", host = HOST, timeout=0.5)
  74.     endTime = time.time()
  75.     print endTime - startTime
  76.     
  77.     print "128.2.203.179:",checkIP("128.2.203.179", host = HOST, timeout = 0.5)
  78.     endTime = time.time()
  79.     print endTime - startTime
  80.